package com.ms.seguridad;

import com.ms.exception.DpwcSecurityException;
import com.ms.seguridad.service.SecurityConstant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.web.DefaultRedirectStrategy;
import org.springframework.security.web.RedirectStrategy;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * User: AW
 * Date: 24/02/13
 */
public class DpwcRedirectStrategy extends DefaultRedirectStrategy implements RedirectStrategy {
    private static final Logger LOG = LoggerFactory.getLogger(DpwcRedirectStrategy.class);

    @Override
    public void sendRedirect(HttpServletRequest request, HttpServletResponse response, String url) throws IOException {
        String urlToRedirect = "";
        String pagesFolder = SecurityConstant.PAGES_FOLDER;
        try {
            urlToRedirect = pagesFolder + "fnTeApuesto.jsf";
//            urlToRedirect = pagesFolder + SecurityConstant.MAIN_PAGE;
            if (urlToRedirect == null) {
                throw new DpwcSecurityException("Configure adecuadamente el URL del menu por defecto: <" + ">");
            }
        } catch (DpwcSecurityException e) {
            LOG.warn("Revise los siguientes detalles en configuracion seguridad:", e);
            urlToRedirect = pagesFolder + "fnTeApuesto.jsf";
        }
        LOG.debug("abriendo directamente la pagina:<" + urlToRedirect + ">");
        super.sendRedirect(request, response, urlToRedirect);
    }
}
